﻿/// <reference path="../Controls/BaseComplexValueControl.js" />
/// <reference path="NodePosition.js" />
/// <reference path="TreeNodeContainer.js" />

var $BaseTreeNode = TA.Turbo.Controls.BaseTreeNode = function BaseTreeNode()
{
	if (Function.IsInherit(arguments)) return;

	this._elements = new Array();
	this._children = new $TreeNodeCollection();
}
$BaseTreeNode.Inherit($TreeNodeContainer);
$BaseTreeNode.Inherit(BaseJsonSerializable);
$BaseTreeNode.Inherit(BaseJsonDeserializable);

var ____a;
if (____a)
{
	$BaseTreeNode.prototype = new $TreeNodeContainer();
	$BaseTreeNode.prototype.TreeNodeContainer = new $TreeNodeContainer();

	$BaseTreeNode.prototype._owner = null;

	$BaseTreeNode.prototype._children = new $TreeNodeCollection();

	$BaseTreeNode.prototype._elements = new Array();
	$BaseTreeNode.prototype._divNode = document.createElement("div");
	$BaseTreeNode.prototype._imgToggle = document.createElement("div");
	$BaseTreeNode.prototype._divContent = document.createElement("div");
	$BaseTreeNode.prototype._divSubList = document.createElement("div");
	$BaseTreeNode.prototype._divLoadChildren = document.createElement("div");
	$BaseTreeNode.prototype._anchorLoadChildren = document.createElement("a");
}

$BaseTreeNode.get_TreeNode = function get_TreeNode()
{
	return this._treeNode;
}


$BaseTreeNode.prototype.NumChildren = 0;
$BaseTreeNode.prototype.UniqueId = null;
$BaseTreeNode.prototype.Position = null;
$BaseTreeNode.prototype.AutoLoadChildren = true;

$BaseTreeNode.prototype._owner = null;
$BaseTreeNode.prototype._expanded = false;

$BaseTreeNode.prototype.get_NodeCollection = function get_NodeCollection()
{
	return this._children;
}
$BaseTreeNode.prototype.get_ContainerElement = function get_ContainerElement()
{
	return this._divSubList;
}

$BaseTreeNode.prototype.get_Children = function get_Children()
{
	return this._children;
}
$BaseTreeNode.prototype.get_Owner = function get_Owner()
{
	return this._owner;
}
$BaseTreeNode.prototype.get_DivNode = function get_DivNode()
{
	return this._divNode;
}
$BaseTreeNode.prototype.get_ImgToggle = function get_ImgToggle()
{
	return this._imgToggle;
}
$BaseTreeNode.prototype.get_DivContent = function get_DivContent()
{
	return this._divContent;
}
$BaseTreeNode.prototype.get_DivSubList = function get_DivSubList()
{
	return this._divSubList;
}
$BaseTreeNode.prototype.get_DivLoadChildren = function get_DivLoadChildren()
{
	return this._divLoadChildren;
}
$BaseTreeNode.prototype.get_AnchorLoadChildren = function get_AnchorLoadChildren()
{
	return this._anchorLoadChildren;
}
$BaseTreeNode.prototype.get_Type = function get_Type()
{
}
$BaseTreeNode.prototype.get_Expanded = function get_Expanded()
{
	return this._expanded;
}
$BaseTreeNode.prototype.set_Expanded = function set_Expanded(value)
{
	this._expanded = value;

	this.UpdateDisplay();

	if (this.get_Expanded())
	{
		if (this.get_Children().length < this.NumChildren)
		{
			if (this.AutoLoadChildren)
			{
				this.DoLoadChildren();
			}
		}
	}
	else
	{
	}
}

$BaseTreeNode.prototype.DoLoadChildren = function DoLoadChildren()
{
	var exclude = new Array();
	var c = this.get_Children(), cl = c.length;
	for (var i = 0; i < cl; ++i)
	{
		exclude.push(c[i].UniqueId);
	}

	var sb = new StringBuilder();
	var jw = new JsonWriter(sb);
	jw.StartObject();
	jw.Property("N", this.NumChildren);
	jw.Property("I", this.UniqueId);
	jw.Property("X", exclude);
	jw.EndObject();

	this.get_Owner().RaiseServerEvent("LoadNodes", sb.toString(), this._loadNodes_Callback.bind(this));
}
$BaseTreeNode.prototype.ExpandCollapse = function ExpandCollapse()
{
//this.set_Expanded
	this.set_Expanded(!this.get_Expanded());
}

$BaseTreeNode.prototype._LinkElements = function _LinkElements()
{
	this._elements.push(this._divNode);
	this._elements.push(this._imgToggle);
	this._elements.push(this._divContent);
	this._elements.push(this._divSubList);
	this._elements.push(this._divLoadChildren);
	this._elements.push(this._anchorLoadChildren);

	for (var iElem = 0; iElem < this._elements.length; ++iElem)
	{
		var elem = this._elements[iElem];

		elem._treeNode = this;
		elem.get_TreeNode = $BaseTreeNode.get_TreeNode;
	}

	this._imgToggle.AddEvent("click", this._imgToggle_Click, this);
	this._anchorLoadChildren.AddEvent("click", this._aLoadChildren_Click, this);
}
$BaseTreeNode.prototype.Parse = function Parse(owner, elem)
{
	this._owner = owner;

	this._divNode = elem;
	this._imgToggle = this._divNode.FindFirstChild("IMG");
	this._divContent = this._divNode.FindFirstChild("DIV.content");
	this._divSubList = this._divNode.FindFirstChild("DIV.tv_list");
	this._divLoadChildren = this._divNode.FindFirstChild("DIV.load_children");
	this._anchorLoadChildren = this._divLoadChildren.FindFirstChild("A");

	this._ParseContent();

	this._ParseChildren();

	this._LinkElements();
}
$BaseTreeNode.prototype.Create = function Create(owner, containerElement, refElement, position)
{
	this._owner = owner;
	this.Position = position;

	this._divNode = document.createElement("DIV");
	this._divNode.AddClass("tv_node");
	containerElement.insertBefore(this._divNode, refElement);

	this._imgToggle = document.createElement("IMG");
	this._divNode.appendChild(this._imgToggle);

	this._divContent = document.createElement("DIV");
	this._divContent.AddClass("content");
	this._divNode.appendChild(this._divContent);

	this._divLoadChildren = document.createElement("DIV");
	this._divLoadChildren.AddClass("load_children");
	this._divNode.appendChild(this._divLoadChildren);

	this._anchorLoadChildren = document.createElement("A");
	this._anchorLoadChildren.href = "";
	this._anchorLoadChildren.innerHTML = "More ...";
	this._divLoadChildren.appendChild(this._anchorLoadChildren);

	this._divSubList = document.createElement("DIV");
	this._divSubList.AddClass("tv_list");
	this._divNode.appendChild(this._divSubList);

	this._CreateContent();
	this._CreateChildren();

	this._LinkElements();
	this.UpdateDisplay();
}
$BaseTreeNode.prototype._RemoveElements = function _RemoveElements()
{
	for (var iElem = 0; iElem < this._elements.length; ++iElem)
	{
		var elem = this._elements[iElem];

		elem.parentNode.removeChild(elem);
	}
}
$BaseTreeNode.prototype._ParseContent = function _ParseContent()
{
}
$BaseTreeNode.prototype._CreateContent = function _CreateContent()
{
}

$BaseTreeNode.prototype._imgToggle_Click = function _imgToggle_Click(e)
{
	this.ExpandCollapse();
}
$BaseTreeNode.prototype._aLoadChildren_Click = function _aLoadChildren_Click(e)
{
	this.DoLoadChildren();
	Events.Cancel(e);
}
$BaseTreeNode.prototype._loadNodes_Callback = function _loadNodes_Callback(e)
{
	var t = new $TreeNodeCollection();
	t.Deserialize(e.Main);

	var c = this.get_Children(), cl = c.length;
	for (var i = 0; i < t.length; ++i)
	{
		var node = t[i];

		for (var iExisting = 0; iExisting < cl; ++iExisting)
		{
			if (c[iExisting].UniqueId == node.UniqueId)
			{
				this.RemoveNode(c[iExisting]);
				--iExisting;
				break;
			}
		}
	}

	for (var i = 0; i < t.length; ++i)
	{
		var node = t[i];

		this.AddNode(node);
	}

	this.UpdateDisplay();
}

$BaseTreeNode.prototype.UpdateDisplay = function UpdateDisplay()
{
	this._divSubList.style["display"] = this.get_Expanded() ? "" : "none";
	this.get_DivLoadChildren().style["display"] = this.get_Expanded() && this.NumChildren > this.get_Children().length ? "block" : "none";

	if (this.get_Children().length > 0 || this.NumChildren > 0)
	{
		if (this.get_Expanded())
			this._imgToggle.src = __TVExpanded;
		else
			this._imgToggle.src = __TVCollapsed;
	}
	else
	{
		this._imgToggle.src = __TVLeaf;
	}

	if (this.Position != null)
	{
		this.get_DivNode().className = "tv_node";
		switch (this.Position)
		{
			case TreeNodePosition.RootFirst:
				this.get_DivNode().AddClass("first");
				break;
			case TreeNodePosition.RootLast:
			case TreeNodePosition.SubOnly:
			case TreeNodePosition.SubLast:
				this.get_DivNode().AddClass("last");
				break;
			case TreeNodePosition.RootRegular:
			case TreeNodePosition.SubFirst:
			case TreeNodePosition.SubRegular:
				break;
			case TreeNodePosition.RootOnly:
				this.get_DivNode().AddClass("only");
				break;
		}
	}
}



$BaseTreeNode.prototype.SerializeContents = function SerializeContents(jw)
{
	jw.Property("Y", this.get_Type());
	if (this.NumChildren > 0)
	{
		jw.Property("N", this.NumChildren);
	}

	if (this.UniqueId != null)
	{
		jw.Property("I", this.UniqueId);
	}

	if (this.get_Children().length > 0)
	{
		jw.PropertyName("C");
		this.get_Children().Serialize(jw);
	}

	if (this.get_Expanded())
	{
		jw.Property("E", this.get_Expanded());
	}
}
$BaseTreeNode.prototype.DeserializeContent = function DeserializeContent(propShortName, value)
{
	switch (propShortName)
	{
		case "N": this.NumChildren = value; break;
		case "I": this.UniqueId = value; break;
		case "C":
			var c = new $TreeNodeCollection();
			c.Deserialize(value);
			this._children = c;
			break;
		case "E": this._expanded = value; break;
	}
}



